import java.util.Stack;

public class Sort {
    //插入排序
    /*
    * 时间复杂度:O(N^2)
    * 空间复杂度:O(1)
    * */
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0 ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    //希尔排序
    /*
     * 时间复杂度:O(N^1.3)
     * 空间复杂度:O(1)
     * */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
    }
    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0 ; j-=gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
    //选择排序
    /*
     * 时间复杂度:O(N^2)
     * 空间复杂度:O(1)
     * */
    public static void selectSort(int[] array) {
        int len = array.length;
        for (int i = 0; i < len - 1; i++) {
            int tmp = i;
            for (int j = i+1; j < len - 1; j++) {
                if(array[tmp] > array[j]) {
                    tmp = j;
                }
            }
            int cur = array[i];
            array[i] = array[tmp];
            array[tmp] = cur;
        }
    }
    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i < right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if(left == maxIndex) {
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }
    public static void swap(int[] array, int i, int j) {
        int cur = array[i];
        array[i] = array[j];
        array[j] = cur;
    }
    public static void heapSort(int[] array) {
        createHeap(array);
        int end = array.length-1;
        while (end > 0) {
            swap(array,0,end);
            siftDown(array,0,end);
            end--;
        }
    }
    public static void createHeap(int[] array) {
        for (int parent = (array.length-1-1)/2; parent >= 0 ; parent--) {
            siftDown(array,parent,array.length);
        }
    }
    public static void siftDown(int[] array, int parent, int len) {
        int child = 2*parent+1;
        while (child<len) {
            if (child+1 < len && array[child] < array[child+1]) {
                child = child+1;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }

        }
    }

    //快速排序
    /*
     * 时间复杂度:O(N*log2(N))
     * 空间复杂度:O(log(N))
     * */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    public static void quick(int[] array, int start, int end) {
        if(start >= end) {
            return;
        }
        //优化2：递归得到的数组长度小的时候直接采用选择排序
        if(end-start+1 <= 10) {
            insertSort2(array,start,end);
            return;
        }
        //优化1：三数取中
        int mid = middleNum(array,start,end);
        swap(array,start,mid);

        int pivot = partition(array,start,end);

        quick(array,start,pivot-1);

        quick(array,pivot+1,end);
    }
    //优化1：三数取中
    public static int middleNum(int[] array, int left, int right) {
        int mid = left+((right-left) >> 1);
        if (array[left] > array[right]) {
            if(array[mid] < array[right]) {
                return right;
            }else if(array[mid] > array[left]) {
                return left;
            }else {
                return mid;
            }
        }else {
            if(array[mid] < array[left]) {
                return left;
            }else if(array[mid] > array[right]) {
                return right;
            }else {
                return mid;
            }
        }
    }
    //优化2：递归得到的数组长度小的时候直接采用选择排序
    public static void insertSort2(int[] array,int start,int end) {
        for (int i = start+1; i < end; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= start ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    //方法1  Hoare法
    public static int partitionHoare(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while (left < right) {
            while (left < right && tmp <= array[right]) {
                right--;
            }
            while (left < right && tmp >= array[left]) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;
    }
    //方法2   挖坑法
    public static int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && tmp <= array[right]) {
                right--;
            }
            if (left >= right) {
                break;
            }
            array[left] = array[right];
            while (left < right && tmp >= array[left]) {
                left++;
            }
            if (left >= right) {
                break;
            }
            array[right] = array[left];
        }
        array[right] = tmp;
        return left;
    }
    //方法3   前后指针法
    private static int partition2(int[] array, int left, int right) {
        int prev = left;
        int cur = left+1;
        while (cur < right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }
    //快速排序(非递归)
    public static void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length-1;
        int pivot = partition(array,left,right);

        if(pivot-1 > left) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot+1 < right) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array,left,right);

            if(pivot-1 > left) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1 < right) {
                stack.push(pivot+1);
                stack.push(right);
            }

        }
    }
    //归并排序
    /*
     * 时间复杂度:O(N*log2(N))
     * 空间复杂度:O(N)
     * */
    //1.递归
    public static void mergeSort(int[] array) {
        mergeFunc(array,0,array.length-1);
    }
    //2.非递归
    public static void mergeSortNor(int[] array) {
        int gap = 1;
        while (gap <= array.length) {
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }
    public static void mergeFunc(int[] array,int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = left + ((right - left) >> 1);

        mergeFunc(array,left,mid);

        mergeFunc(array,mid+1,right);

        merge(array,left,mid,right);
    }
    public static void merge(int[] array,int left, int mid, int right) {

        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;

        int[] tmpArr = new int[right-left+1];
        int k= 0;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] <= array[s2]) {
                tmpArr[k++] = array[s1++];
            }else {
                tmpArr[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmpArr[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmpArr[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmpArr[i];
        }
    }
    public static void countSort(int[] array) {
        int max = array[0];
        int min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max < array[i]) {
                max = array[i];
            }
            if (min > array[i]) {
                min = array[i];
            }
        }
        int[] tmp = new int[max-min+1];
        for (int i = 0; i < array.length; i++) {
            int index = array[i]-min;
            tmp[index]++;
        }
        int k = 0;
        for (int i = 0; i < tmp.length; i++) {
            for (int j = 0; j < tmp[i]; j++) {
                array[k++] = i+min;
            }
        }
    }
}
